﻿procedure;
var
  MaSu_DS1, MaSu_DS2: TAppDataSet;
  idPer: string;
  LuP_DSLang, LuP_DSDodPod, LuP_DSSkup : TAppDataSet;
  LuP_IDNew, LuP_IDOld, LuP_LangCZ, LuP_LangSK, LuP_LangEN, LuP_Value, LuP_Code : String;
  
begin
  AppServer.SetDSTransferData(SourceName,'6269,5',nttNonLicAndNonNLP,lttAll,[mtaInsert,mtaUpdate,mtaDelete],[mtaInsert]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttNonLicAndNLP,lttNonLangOrNonNLP,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttLicAndNonNLP,lttLangOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttLicAndNLP,lttNonNLPOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'6269,5',nttNLPOnly,lttNLPOnly,[mtaIUpdate],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttNonLicAndNonNLP,lttAll,[mtaInsert,mtaUpdate,mtaDelete],[mtaInsert]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttNonLicAndNLP,lttNonLangOrNonNLP,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttLicAndNonNLP,lttLangOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttLicAndNLP,lttNonNLPOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'9999,9999',nttNLPOnly,lttNLPOnly,[mtaIUpdate],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttNonLicAndNonNLP,lttAll,[mtaInsert,mtaUpdate,mtaDelete],[mtaInsert]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttNonLicAndNLP,lttNonLangOrNonNLP,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttLicAndNonNLP,lttLangOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttLicAndNLP,lttNonNLPOnly,[mtaInsert,mtaUpdate,mtaDelete],[]);
  AppServer.SetDSTransferData(SourceName,'165587,10',nttNLPOnly,lttNLPOnly,[mtaIUpdate],[]);
  M17059359_10;  // NEMAZAT, STANDARDNI SOUCAST REINSTALACE
  AppServer.SetSQLScript(SourceName,TargetPath + 'Reinstall_1171AD.sql');
  
  //MaSu - plnění atributu "Čas zápisu problému" v protokolech WF - NESPOUŠTĚT
  MaSu_DS1 := TAppDataSet.Create;
  MaSu_DS2 := TAppDataSet.Create;
  try
    //Protokoly workflow
    MaSu_DS1.SetDSParams(SourceName,rmPrimary,'40046411,10', dsaReadNonVisible);
    MaSu_DS1.Filter := '(("40565773,10" = 1) or ("40565773,10" = 2)) and ("42701639,10" is null)';
    MaSu_DS1.NoMacros := True;
    MaSu_DS1.Open;
    
    //Protokoly procesu
    MaSu_DS2.SetDSParams(SourceName,rmPrimary,'40046530,10', dsaReadNonVisible);
    MaSu_DS2.NoMacros := True;
    MaSu_DS2.OpenEmpty;
    MaSu_DS2.MaxRecordCount := 1;
    MaSu_DS2.OrderBy := '"40048771,10" DESC';
    
    while not MaSu_DS1.EOF do
    begin
      MaSu_DS2.Filter := Format('(("40048772,10" = 1) or ("40048772,10" = 2)) and ("40048771,10" is not null) and
                             ("ic_b" = %%) and ("u_b" = %%)',
                            [LByID(MaSu_DS1.GetPKValue),RByID(MaSu_DS1.GetPKValue)]);
      if not MaSu_DS2.IsEmpty then
        //"Čas zápisu problému"
        AppServer.SetDBAtrValue(SourceName,'','2396269,10','42701637,10',MaSu_DS1.GetPKValue,MaSu_DS2.FieldByName('40048771,10').AsANSIString);
      MaSu_DS1.Next;
    end;
  finally
    MaSu_DS1.Free;
    MaSu_DS2.Free;
  end;
  //MaSu - END
  
  // ZS % povinny prispevek - nastaveni hodnoty v Konfiguraci mezd (NESPOUSTET)
  if (Legislation = '745998,10') then
  begin
    idPer := AppServer.GetDBKeyValues(SourceName, rmPrimary, '450391,10', '("546181,10" >= ''2026-01-01'') AND ("451082,10" = 0) AND ("12685418,10" <> 4)');
    if (idPer <> '') then
      AppServer.SetDBAtrValue(SourceName, UserLang, '450391,10', '12685418,10', idPer, '4');
  end;
  // ZS
  
  // ZS navrat nastaveni puvodni hodnoty zivotniho minima jednortlivce od 5.2026 (NESPOUSTET)
  // chybne nastavena na 5500 v reinstalaci 117.0
  if (Legislation = '745998,10') then
  begin
    idPer := AppServer.GetDBKeyValues(SourceName, rmPrimary, '450391,10', '("546181,10" >= ''2026-05-01'') AND ("451082,10" = 0)');
    if (idPer <> '') then
      if (AppServer.GetDBAtrValue(SourceName, rmPrimary, '', '450391,10', '2497496,10', idPer) = '5500') then
        // konfigurace mezd - Zivotni minimum jednotlivce
        AppServer.SetDBAtrValue(SourceName, UserLang, '450391,10', '2497496,10', idPer, '4860');
  end;
  // ZS konec
  
  // LuP 01 - begin : zalozeni a slouceni distribuovanych dodacich podminek v uzlu DB (NESPOUSTET)
  LuP_DSLang := TAppDataSet.Create;
  try
    LuP_DSLang.SetDSParams(SourceName, rmPrimary, '911,0', dsaReadNonVisible);
    LuP_DSLang.OpenEmpty;
    if MainLang <> '401,0' then begin
      LuP_DSLang.Filter := '("ic" = 401 and "u" = 0)'; //CZ
      if not LuP_DSLang.IsEmpty then 
        LuP_LangCZ := '401,0';
    end;
    //
    if MainLang <> '403,0' then begin
      LuP_DSLang.Filter := '("ic" = 403 and "u" = 0)'; //SK
      if not LuP_DSLang.IsEmpty then
        LuP_LangSK := '403,0';
    end;
    //
    if MainLang <> '402,0' then begin
      LuP_DSLang.Filter := '("ic" = 402 and "u" = 0)'; //EN
      if not LuP_DSLang.IsEmpty then
        LuP_LangEN := '402,0';
    end;
  finally
    LuP_DSLang.Free;
  end;
  //
  LuP_DSSkup := TAppDataSet.Create;
  try
    LuP_DSSkup.SetDSParams(SourceName, rmPrimary, '6084529,10', dsaFullNonVisible);
    LuP_DSSkup.Filter := '("u"=9)';
    LuP_DSSkup.OrderBy := '"6084553,10"';
    LuP_DSSkup.Open;
    while not LuP_DSSkup.EOF do begin
      LuP_DSSkup.DuplicateRow;
      LuP_DSSkup.PostNoConfirm;
      LuP_IDNew := LuP_DSSkup.GetKeyValue('', ftID);
      AppServer.SetDBAtrValue(SourceName, '', '6084471,10', '42716004,10', LuP_IDNew, '1');
      //
      LuP_DSSkup.Next;
      LuP_IDOld := LuP_DSSkup.GetKeyValue('', ftID);       
      //
      case
        LuP_LangCZ <> '' :
        begin
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangCZ, '6084471,10', '6084516,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangCZ, '6084471,10', '6084516,10', LuP_IDNew, LuP_Value);
        end;
        
        LuP_LangSK <> '' :
        begin
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangSK, '6084471,10', '6084516,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangSK, '6084471,10', '6084516,10', LuP_IDNew, LuP_Value);
        end;
        
        LuP_LangEN <> '' :
        begin
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangEN, '6084471,10', '6084516,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangEN, '6084471,10', '6084516,10', LuP_IDNew, LuP_Value);
        end;
      end;
      //
      AppServer.MergeDBRecords(SourceName, '6084471,10', LuP_IDNew, LuP_IDOld, true);
      //
      LuP_DSSkup.Next;
    end;
    //
  finally
    LuP_DSSkup.Free;
  end;
  LuP_DSDodPod := TAppDataSet.Create;
  try
    LuP_DSDodPod.SetDSParams(SourceName, rmPrimary, '1119637,10', dsaFullNonVisible);
    LuP_DSDodPod.Filter := '("u"=9)';
    LuP_DSDodPod.OrderBy := '"1119639,10"';
    LuP_DSDodPod.Open;
    while not LuP_DSDodPod.EOF do begin
      LuP_DSDodPod.DuplicateRow;
      LuP_DSDodPod.PostNoConfirm;
      LuP_IDNew := LuP_DSDodPod.GetKeyValue('', ftID);
      AppServer.SetDBAtrValue(SourceName, '', '1119632,10', '42716006,10', LuP_IDNew, '1');
      //
      LuP_DSDodPod.Next;
      LuP_IDOld := LuP_DSDodPod.GetKeyValue('', ftID);       
      //
      case
        LuP_LangCZ <> '' :
        begin
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangCZ, '1119632,10', '1119633,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangCZ, '1119632,10', '1119633,10', LuP_IDNew, LuP_Value);
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangCZ, '1119632,10', '39455819,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangCZ, '1119632,10', '39455819,10', LuP_IDNew, LuP_Value);
        end;
        
        LuP_LangSK <> '' :
        begin
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangSK, '1119632,10', '1119633,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangSK, '1119632,10', '1119633,10', LuP_IDNew, LuP_Value);
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangSK, '1119632,10', '39455819,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangSK, '1119632,10', '39455819,10', LuP_IDNew, LuP_Value);
        end;
        
        LuP_LangEN <> '' :
        begin
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangEN, '1119632,10', '1119633,10', LuP_IDOld);
          if LuP_Value = '' then begin
            LuP_Code := LuP_DSDodPod.FieldByName('1119639,10').AsString;
            case
              LuP_Code = 'CFR' :
                LuP_Value := 'Cost and Freight';
              LuP_Code = 'CIF' :
                LuP_Value := 'Cost, Insurance and Freight';
              LuP_Code = 'CIP' :
                LuP_Value := 'Carriage and Insurance Paid To';
              LuP_Code = 'CPT' :
                LuP_Value := 'Carriage Paid To';
              LuP_Code = 'DAP' :
                LuP_Value := 'Delivered at Place';
              LuP_Code = 'DDP' :
                LuP_Value := 'Delivered Duty Paid';
              LuP_Code = 'DPU' :
                LuP_Value := 'Delivered at Place Unloaded';
              LuP_Code = 'EXW' :
                LuP_Value := 'Ex Works';
              LuP_Code = 'FAS' :
                LuP_Value := 'Free Alongside Ship';
              LuP_Code = 'FCA' :
                LuP_Value := 'Free Carrier';
              LuP_Code = 'FOB' :
                LuP_Value := 'Free On Board';
              LuP_Code = 'XXX' :
                LuP_Value := 'Other delivery terms';
            end;
          end;
          AppServer.SetDBAtrValue(SourceName, LuP_LangEN, '1119632,10', '1119633,10', LuP_IDNew, LuP_Value);
          //
          LuP_Value := AppServer.GetDBAtrValue(SourceName, rmPrimary, LuP_LangEN, '1119632,10', '39455819,10', LuP_IDOld);
          AppServer.SetDBAtrValue(SourceName, LuP_LangEN, '1119632,10', '39455819,10', LuP_IDNew, LuP_Value);
        end;
      end;
      //
      AppServer.MergeDBRecords(SourceName, '1119632,10', LuP_IDNew, LuP_IDOld, true);
      //
      LuP_DSDodPod.Next;
    end;
    //zruseni kaskadniho mazani na tride "Ciselnik dodacich podminek"
    AppServer.SetDBAtrValue(SourceName, '', '101,0', '204,1', '1119632,10', '0');
  finally
    LuP_DSDodPod.Free;
  end;
  // LuP 01 - end
end.
